package com.bookstore.repository;

import com.bookstore.entity.UserHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserHistoryRepository extends JpaRepository<UserHistory, Long> {
    
    List<UserHistory> findByUserIdOrderByCreatedAtDesc(Long userId);
    
    @Query("SELECT h.categoryId FROM UserHistory h WHERE h.userId = :userId GROUP BY h.categoryId ORDER BY COUNT(h.id) DESC")
    List<Long> findTopCategoriesByUserId(@Param("userId") Long userId);
    
    @Query("SELECT h.bookId FROM UserHistory h WHERE h.userId = :userId GROUP BY h.bookId ORDER BY COUNT(h.id) DESC")
    List<Long> findTopBooksByUserId(@Param("userId") Long userId);
}